#coding=utf-8
from datetime import datetime, timedelta
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from log import *

def download_excel():
    #实例化浏览器
    driver = webdriver.Chrome()
    driver.maximize_window()
    #打开平台网址
    driver.get("https://sso.bidcenter.com.cn/login/")
    driver.implicitly_wait(5)
    #登录系统
    driver.find_element(By.XPATH,"//input[@id='txtusername']").send_keys("SZDK")
    driver.find_element(By.XPATH,"//input[@id='txtpassword']").send_keys("Derkee2023")
    driver.find_element(By.XPATH,"//a[@id='login_login_btn']").click()
    time.sleep(1)
    #找到页面上的SZDK，切换到“我是供应商”用户中心
    driver.find_element(By.XPATH,"//span[text()='SZDK' and @title='SZDK']").click()
    driver.find_element(By.XPATH,"//a[@title='我是供应商']").click()
    time.sleep(3)
    #获取浏览器的句柄，切换到最新窗口
    handles=driver.window_handles
    driver.switch_to.window(handles[-1])
    # 获取当前日期
    current_date = datetime.now().date()
    # 减去一天
    # yesterday = str(current_date - timedelta(days=1))
    yesterday='2024-11-10'
    #选择开始日期
    driver.find_element(By.XPATH,'//*[@id="pane-1"]/div/div[2]/div/div[2]/div/span[2]/span/button/span/div/span').click()
    driver.find_element(By.XPATH,'/html/body/div[2]/div[1]/div/div[2]/div[2]/div/input[1]').clear()
    driver.find_element(By.XPATH,'/html/body/div[2]/div[1]/div/div[2]/div[2]/div/input[1]').send_keys(yesterday)
    time.sleep(1)
    # 选择结束日期
    driver.find_element(By.XPATH,'/html/body/div[2]/div[1]/div/div[2]/div[2]/div/input[2]').clear()
    driver.find_element(By.XPATH,'/html/body/div[2]/div[1]/div/div[2]/div[2]/div/input[2]').send_keys(yesterday)
    time.sleep(1)
    #点击确定按钮，触发查询
    driver.find_element(By.XPATH,'/html/body/div[2]/div[1]/div/div[2]/div[1]').click()
    driver.find_element(By.XPATH,'/html/body/div[2]/div[1]/div/div[3]/button[2]').click()
    time.sleep(1)
    #点击页面全选按钮，选中当前页所有的数据
    driver.find_element(By.XPATH,'//*[@id="pane-1"]/div/div[3]/div[1]/div[2]/table/thead/tr/th[1]/div/label/span/span').click()
    #进入循环
    #判断筛选结果是否有下一页内容：
    # 如果有，就继续点击下一页，然后点全选按钮；
    # 如果没有，则进入批量导出页面，将数据导下来
    while True:
        target = driver.find_element(By.XPATH, '/html/body/div[1]/div/div[3]/section/div/div[1]/div[1]/div/div/div[2]/div[1]/div/div[3]/div[2]/div[2]/div/div/button[2]')

        # 检查下一页按钮是否可见且可点击
        if target.is_displayed() and target.is_enabled():
            # 点击下一页按钮
            target.click()
            logger.info('Clicked next page.')
            time.sleep(3)
            driver.find_element(By.XPATH,'//*[@id="pane-1"]/div/div[3]/div[1]/div[2]/table/thead/tr/th[1]/div/label/span/span').click()
            time.sleep(1)
        else:
            # 如果下一页按钮不可点击，下载数据，结束循环
            logger.info('No more pages to click.')
            driver.find_element(By.XPATH, "//i[text()='批量导出']").click()
            time.sleep(5)
            driver.find_element(By.XPATH, "//h2[text()='普通字段']").click()
            time.sleep(100000)
            logger.info('开始下载excel数据，请等待...')
            driver.find_element(By.XPATH, "//button[text()=' 立即导出 ']").click()
            time.sleep(10)
            logger.info('excel数据下载完成！！！')
            break

if __name__ == '__main__':

    download_excel()